# coding=utf-8
import requests
import sys
import re
from bs4 import BeautifulSoup
from mysqler import Mysql

reload(sys)
sys.setdefaultencoding('utf-8')

class BigNr():


	def __init__(self):
		self.__mysql =Mysql()#实例化对象 赋值到__mysql

	def UR(self,i):#这里去的网页的内容
		headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'}

		proxies={
			'http':'socks5://127.0.0.1:1080',
			'https':'socks5://127.0.0.1:1080'
		}

		try:
			url =requests.get(url=i.strip('\n'),headers=headers,timeout=120)#.strip('\n'),proxies=proxies
			url.keep_alive = False
		except Exception,e:
			print e
			print "超时！！！"
			exit()
		nr =url.text
		return nr
	def Nr(self,nr,i):#通过正则取到所有的信息
		bsobj =BeautifulSoup(nr)
		'''得到视频'''
		mp4 = bsobj.html.findAll("source",{"type":"video/mp4"})
		#mp4 = bsobj.html.findAll(id="player")
		mp5 =str(mp4)
		r = r'res="360" src="(.*?)" type="video/mp4"'
		rar =re.compile(r,re.S)
		img = re.findall(rar,mp5)
		for mp in img:
			print mp #视频地址
			'''得到纯标题'''
			tit =r'http://.*?/.*?/(.*?)/'
			tit1 =re.compile(tit)
			title =re.findall(tit1,i)
			title1 = title[0]
			print str(title1)

			'''得到图片'''
			r2 = r'<meta property="og:image" content="(.*?)">'#这里取得图片
			rar2 =re.compile(r2,re.S)
			imges =re.findall(rar2,nr)
			for jpg in imges:
				print jpg

			'''判断爬取链接和视频链接是否一致。是，存入数据'''
			r =r'http://.*?/(.*?)/'#链接
			img = re.compile(r,re.S)
			s =re.findall(img,i)
			r2 = r'http://.*?/file/(.*?)/'#视频
			img2 =re.compile(r2)
			s2 =re.findall(img2,mp)
			#print s,s2
			if s == s2:
				self.__mysql.Get_sql(jpg,i,title1,mp)#调用Mysql实例的方法
			else:
				print "不一致！"
				exit()

# wz =BigNr()
#
# i= "http://www.baidu.com"
# url =wz.UR(i)
#
# mp4 =wz.Nr(url,i)


